package zhuhai.tedu.parttimess.service.impl;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import zhuhai.tedu.parttimess.domain.Result;
import zhuhai.tedu.parttimess.domain.Student;
import zhuhai.tedu.parttimess.domain.User;
import zhuhai.tedu.parttimess.exception.PasswordNotMatchException;
import zhuhai.tedu.parttimess.exception.UserNotFoundException;
import zhuhai.tedu.parttimess.repository.BaseRepository;
import zhuhai.tedu.parttimess.repository.StudentRepository;
import zhuhai.tedu.parttimess.repository.UserRepository;
import zhuhai.tedu.parttimess.service.UserService;
import zhuhai.tedu.parttimess.utils.ExcelUtils;
import zhuhai.tedu.parttimess.utils.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


@Service
public class UserServiceImpl extends CommonServiceImpl<User,Integer> implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public BaseRepository<User, Integer> currentRepository() {
        return userRepository;
    }

    @Override
    public User findByName(String name) {
        return userRepository.findByName(name);
    }


    @Override
    public User save(User user) {
        String salt = UUID.randomUUID().toString().toUpperCase();
        user.setSalt(salt);
        String srcPassword=user.getPassword();
        String md5Password=getMd5Password(srcPassword,salt);
        user.setPassword(md5Password);
        return super.save(user);
    }

    //对密码进行加盐
    private String getMd5Password(
            String srcPassword, String salt) {
        String str = salt + srcPassword + salt;
        for (int i = 0; i < 10; i++) {
            str = DigestUtils
                    .md5DigestAsHex(str.getBytes())
                    .toUpperCase();
        }
        return str;
    }

    public User login(String name,String password){
        User data = findByName(name);
        if (data == null) {
            System.out.println("用户不存在");
            throw new UserNotFoundException(
                    "登录失败！您尝试登录的用户名不存在！");
        }else {
            String salt = data.getSalt();
            String md5Password
                    = getMd5Password(password, salt);
            if (data.getPassword().equals(md5Password)) {
                return data;
            }else {
                throw new PasswordNotMatchException(
                        "登录失败！密码错误！");
            }
        }

    }

}
